package com.sean.findme.dbentrys;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.content.Context;

public class SAXDatabaseService {
	private Context mContext;
	private SAXDatabaseHandler handler = null;
	private String fileName;
	
	private static SAXDatabaseService instance;
	

	public SAXDatabaseService(Context mContext, String fileName ) {
		this.mContext = mContext;
		this.fileName = fileName;
		instance = this;
	}
	
	public static SAXDatabaseService getInstance()
	{
		if( instance == null )
			instance = new SAXDatabaseService(null, null);
		return instance;
	}
	
	public void parseXML()
	{
		
		try {
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser saxParser = spf.newSAXParser();
			handler = new SAXDatabaseHandler();
			try {
				InputStream input = mContext.getResources().getAssets().open(this.fileName);
				InputSource inputSrc = new InputSource(input);
				inputSrc.setEncoding("UTF-8");
				saxParser.parse(inputSrc, handler);
				input.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public boolean bFinished()
	{
		return this.handler.isbFinished();
	}
	public void showAll()
	{
		this.handler.displayAll();
	}
	

}
